package com.huarui.companyconfig.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.ewaf.framework.service.BaseService;

import com.huarui.companyconfig.dao.ICompanyConfigDao;
import com.huarui.companyconfig.model.CompanyConfig;
import com.huarui.initial.util.InitialErrorsEnum;
import com.huarui.payInvoice.util.PayInvoiceMessageEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class CompanyConfigService extends BaseService {
	private final static Logger logger = Logger.getLogger(CompanyConfigService.class);
	private ICompanyConfigDao getDao(){
		return (ICompanyConfigDao) this.dao;
	}
	
	public List<CompanyConfig> getListByCompanyId(Integer companyId){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("companyId", companyId);
		List<CompanyConfig> result = getDao().getListByConditions(map);
		return result;
	}
	public boolean createDefalutCompanyConfig(Integer companyId){
		List<CompanyConfig> list=new ArrayList<CompanyConfig>();
		list.add(getNewCompanyConfig("retailPrice",CompanyConfig.DEFAULT_RETAILPRICE,companyId));
		list.add(getNewCompanyConfig("insurancePrice",CompanyConfig.DEFAULT_INSURANCE,companyId));
		list.add(getNewCompanyConfig("minPrice",CompanyConfig.DEFAULT_MINPRICE,companyId));
		list.add(getNewCompanyConfig("publicanLevel",CompanyConfig.DEFAULT_PUBLICANLEVEL,companyId));
		try {
			getDao().batchInsert(list);
		} catch (Exception e) {
			logger.error("初始公司默认配置出错",e);
			return false;
		}
		return true;
	}
	@SuppressWarnings("rawtypes")
	public Map<String,Object> batchUpdate(Map<String,String> listMap){
		Map<String,Object> resultMap=new HashMap<String,Object>();
		Map<String,Object> map=new HashMap<String,Object>();
		SessionMessage sm=UserHolder.getCurrentSessionMessage();
		map.put("companyId", sm.getCompanyId());
		try {
			List<CompanyConfig> list=getDao().getListByConditions(map);
			for(Iterator it=listMap.entrySet().iterator();it.hasNext();){
				Map.Entry e=(Entry) it.next();
				String varName=(String) e.getKey();
				String varValue=(String)e.getValue();
				for(CompanyConfig companyConfig:list){
					if(companyConfig.getVarName().equals(varName)){
						companyConfig.setVarValue(varValue);
						getDao().update(companyConfig);
					}
				}
				
			}
		} catch (Exception e) {
			logger.error("根据id获得companyConfig出错",e);
			resultMap.put(InitialErrorsEnum.RESULT_TYPE_FAILURE.getMsg(), InitialErrorsEnum.ERROR_MSG_DATABASE.getMsg());
			return resultMap;
		}
		resultMapInfo(resultMap);
		return resultMap;
	}
	private CompanyConfig getNewCompanyConfig(String varName,String varValue,Integer companyId){
		CompanyConfig companyConfig=new CompanyConfig();
		companyConfig.setCompanyId(companyId);
		companyConfig.setVarName(varName);
		companyConfig.setVarValue(varValue);
		return companyConfig;
	}
	
	private void resultMapInfo(Map<String,Object> resultMap){
		if(!resultMap.containsKey(PayInvoiceMessageEnum.SUCCESS.getMsg())){
			resultMap.put(PayInvoiceMessageEnum.SUCCESS.getMsg(),PayInvoiceMessageEnum.RESULT_TYPE_SUCCESS.getMsg());
		}
	}
}
